<?php

namespace App\Listeners;

use App\Events\MedalEvent;
use App\Models\Member\Medal;
use App\Models\Member\Medals;
use App\Models\Member\Member;
use Illuminate\Support\Facades\Cache;

class MedalListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @return void
     */
    public function handle(MedalEvent $event)
    {
        $member = Member::where('uuid', $event->member_uuid)->with('attrs')->first();
        if(!$member) return;

        if($member->state != 200) return;

        $medalHas = Medals::where('member_uuid', $event->member_uuid)->where('medal_id', $event->medal_id)->first();
        if($medalHas && $medalHas->is_get == 1) return;

        $medal = Medal::where('id', $event->medal_id)->first();
        if(!$medal) return;

        if(!isset($medalHas)){  //当没获得勋章时处理
            $medalHas = Medals::create([
                'member_uuid' => $event->member_uuid,
                'medal_id' => $event->medal_id,
                'is_get' => 0,
                'is_use' => 0,
                'num' => 0,
            ]);
        }

        $is_get = 0;
        switch ($medal->id){
            case 1:
            case 2:
            case 3:
                if($member->level_number >= $medal->num){
                    $is_get = 1;
                    $medalHas->update([
                        'is_get' => $is_get,
                        'num' => $medal->num,
                    ]);
                }
                break;
            case 4:
            case 5:
            case 7:
            case 8:
            case 9:
            case 10:
            case 11:
            case 12:
            case 13:
            case 14:
            case 15:
            case 16:
            case 20:
            case 21:
            case 25:
            case 26:
            case 27:
                $num = $medalHas->num + 1;
                $is_get = $num >= $medal->num ? 1 : 0;
                $medalHas->update([
                    'is_get' => $is_get,
                    'num' => $num,
                ]);
                break;
            case 24:
                if($member->state == 200 && $member->nickname && $member->avatar_url && $member->city_id && $member->introduction && $member->title && $member->desc && $member->background_url && !empty($member->attrs)){
                    $num = $medalHas->num + 1;
                    $is_get = $num >= $medal->num ? 1 : 0;
                    $medalHas->update([
                        'is_get' => $is_get,
                        'num' => $num,
                    ]);
                }
                break;
            default:
                break;

        }

        if($is_get){
            $cacheMedals = Cache::get("medal_".$event->member_uuid);

            $medal['total'] = Medals::where('medal_id', $medal->id)->where('is_get', 1)->count('*');
            $medal['get_time'] = date('Y-m-d H:i:s');
            $cacheMedals[] = $medal;

            Cache::forever("medal_".$event->member_uuid, $cacheMedals);
        }



    }
}
